/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLLERS;

import BEANS.ComboItem;
import UTIL.DatabaseConnector;
import UTIL.InputValidator;
import UTIL.TableManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JDialog;

/**
 *
 * @author Belthazod
 */
public class SupplierController {

    private final DatabaseConnector dbConnector;    
    private TableManager supplierTableManager;
    
    private JComboBox<ComboItem> addProductSupplierComboBox;
    private JComboBox<ComboItem> editProductSupplierComboBox;
    private JComboBox<ComboItem> deliveryProductFilterSupplierComboBox;
    private JComboBox<ComboItem> transferProductFilterSupplierComboBox;
    private JComboBox<ComboItem> editProductFilterSupplierComboBox;
    private JComboBox<ComboItem> deliverySupplierComboBox;
    private JComboBox<ComboItem> productFilterSupplierComboBox;
    private JComboBox<ComboItem> replacementFilterSupplierComboBox;
    private JComboBox<ComboItem> reportsFilterSupplierComboBox;
    private JComboBox<ComboItem> returnsSupplierComboBox;
    private JComboBox<ComboItem> adjustmentsSupplier;
    
    public SupplierController(JTable table, JComboBox<ComboItem> addProductSupplierComboBox,
            JComboBox<ComboItem> editProductSupplierComboBox,
            JComboBox<ComboItem> editProductFilterSupplierComboBox,
            JComboBox<ComboItem> transferProductFilterSupplierComboBox,
            JComboBox<ComboItem> deliveryProductFilterSupplierComboBox,
            JComboBox<ComboItem> productFilterSupplierComboBox,
            JComboBox<ComboItem> replacementFilterSupplierComboBox,
            JComboBox<ComboItem> reportsFilterSupplierComboBox,
            JComboBox<ComboItem> returnsSupplierComboBox,
            JComboBox<ComboItem> adjustmentsSupplier) {
        supplierTableManager = new TableManager(table);
        dbConnector = DatabaseConnector.getInstance();
        this.adjustmentsSupplier = adjustmentsSupplier;
        this.addProductSupplierComboBox = addProductSupplierComboBox;
        this.editProductSupplierComboBox = editProductSupplierComboBox;
        this.editProductFilterSupplierComboBox = editProductFilterSupplierComboBox;        
        this.transferProductFilterSupplierComboBox = transferProductFilterSupplierComboBox;
        this.deliveryProductFilterSupplierComboBox = deliveryProductFilterSupplierComboBox;
        this.productFilterSupplierComboBox = productFilterSupplierComboBox;
        this.replacementFilterSupplierComboBox = replacementFilterSupplierComboBox;
        this.reportsFilterSupplierComboBox = reportsFilterSupplierComboBox;
        this.returnsSupplierComboBox = returnsSupplierComboBox;
    }
    
    public void addSupplier(JTextField name) {
        
        String[] values = {name.getText()};
        String supp = name.getText();
        try {
            if (supp.trim().length() <= 0){
                JOptionPane.showMessageDialog(null, "Supplier Name cannot be empty.", "Invalid Input", JOptionPane.ERROR_MESSAGE);
            }else{
            dbConnector.insert("INSERT INTO Supplier(supplier_name) VALUES(?)", values);
            JTextField[] inputs = {name};
            
            JOptionPane.showMessageDialog(null, name.getText() + " saved to Supplier list.");
            InputValidator.clearInput(inputs);
            dbConnector.closeConnection();
            }
        } catch (SQLException sqlE) {
            JOptionPane op = new JOptionPane("Failed to add supplier.", JOptionPane.ERROR_MESSAGE);
            sqlE.printStackTrace();
            JDialog dialog = op.createDialog("Database Error");
            dialog.setAlwaysOnTop(true);
            dialog.setModal(true);
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
            //JOptionPane.showMessageDialog(null, "Add to suppliers list failed", "Database error", JOptionPane.ERROR_MESSAGE);
        }
        
    }

    public void updateTableContents() {
        try {
            ResultSet rs = dbConnector.query("SELECT supplier_id, supplier_name FROM supplier ORDER BY 2 ASC");
            supplierTableManager.importDBContents(rs);
            updateSupplierComponents();
            dbConnector.closeConnection();
        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
            JOptionPane.showMessageDialog(null, "Table update failed", "Database error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    public void editSupplier(JTextField name, String supplierID) {
        
        if (InputValidator.checkInput(name.getText(), "Supplier Name cannot be empty.")) {
            try {
                String[] values = {name.getText()};
                dbConnector.update("UPDATE supplier SET supplier_name = ? WHERE supplier_id = ?", values, supplierID);
                JTextField[] inputs = {name};
                JOptionPane op = new JOptionPane("Supplier details successfully updated.", JOptionPane.INFORMATION_MESSAGE);
                JDialog dialog = op.createDialog("Edit Supplier");
                dialog.setAlwaysOnTop(true);
                dialog.setModal(true);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setVisible(true);
                //JOptionPane.showMessageDialog(null, "Supplier details updated.", "Success", JOptionPane.INFORMATION_MESSAGE);
                InputValidator.clearInput(inputs);
                dbConnector.closeConnection();
            } catch (SQLException sqlE) {
                JOptionPane op = new JOptionPane("Failed to edit supplier.", JOptionPane.ERROR_MESSAGE);
                JDialog dialog = op.createDialog("Database Error");
                dialog.setAlwaysOnTop(true);
                dialog.setModal(true);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setVisible(true);
                // JOptionPane.showMessageDialog(null, "Edit supplier failed", "Database error", JOptionPane.ERROR_MESSAGE);
            }            
        } else {
            JOptionPane op = new JOptionPane("Please fill out all fields", JOptionPane.ERROR_MESSAGE);
            JDialog dialog = op.createDialog("Input Error");
            dialog.setAlwaysOnTop(true);
            dialog.setModal(true);
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
            //JOptionPane.showMessageDialog(null, "Please fill out all fields", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    public boolean openEditDialog(String supplierID, JTextField[] inputs, JLabel supplierIDTextField) {
        try {
            ResultSet rs = dbConnector.query("SELECT supplier_name FROM supplier WHERE supplier_id = ?", supplierID);
            while (rs.next()) {
                for (int i = 0; i < inputs.length; i++) {
                    inputs[i].setText(rs.getString(i + 1));
                }
            }
            supplierIDTextField.setText(supplierID);
            return true;
            
        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
            JOptionPane.showMessageDialog(null, "Failed to edit supplier details.", "Database error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    
    public void deleteSelectedSupplier() {
        try {
            String supplierID = supplierTableManager.getIDFromTable(supplierTableManager.getSelectedRow());
            dbConnector.delete("DELETE FROM supplier WHERE supplier_id = ?", supplierID);
            JOptionPane.showMessageDialog(null, "Selected supplier was deleted", "Success", JOptionPane.INFORMATION_MESSAGE);
        } catch (SQLException sqlE) {
            if (sqlE.getSQLState().startsWith("23")) {
                JOptionPane op = new JOptionPane("Failed to delete selected supplier. Supplier is currently linked to a product. \n\nTo remove this supplier. Please clear all products associated with it.", JOptionPane.ERROR_MESSAGE);
                JDialog dialog = op.createDialog("Database Error");
                dialog.setAlwaysOnTop(true);
                dialog.setModal(true);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setVisible(true);
                //JOptionPane.showMessageDialog(null, "Failed to delete selected supplier. Supplier is currently linked to a product. \n\nTo remove this supplier. Please clear all products associated with it.", "Database Error", JOptionPane.ERROR_MESSAGE);
            } else {
                sqlE.printStackTrace();
                JOptionPane op = new JOptionPane("Failed to delete selected supplier.", JOptionPane.ERROR_MESSAGE);
                JDialog dialog = op.createDialog("Database Error");
                dialog.setAlwaysOnTop(true);
                dialog.setModal(true);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setVisible(true);
                //JOptionPane.showMessageDialog(null, "Failed to delete selected supplier.", "Database Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    
    public void updateSupplierComponents() {
        try {
            ResultSet rs = dbConnector.query("SELECT Distinct(supplier_id), supplier_name FROM supplier JOIN product USING(supplier_id) ORDER BY 2 ASC");
            addProductSupplierComboBox.removeAllItems();
            editProductSupplierComboBox.removeAllItems();
            editProductFilterSupplierComboBox.removeAllItems();
            transferProductFilterSupplierComboBox.removeAllItems();
            deliveryProductFilterSupplierComboBox.removeAllItems();
            productFilterSupplierComboBox.removeAllItems();
            replacementFilterSupplierComboBox.removeAllItems();
            reportsFilterSupplierComboBox.removeAllItems();
            adjustmentsSupplier.removeAllItems();
            //returnsSupplierComboBox.removeAllItems();
            
            editProductFilterSupplierComboBox.addItem(new ComboItem(null, "All"));
            transferProductFilterSupplierComboBox.addItem(new ComboItem(null, "All"));
            productFilterSupplierComboBox.addItem(new ComboItem(null, "All"));
            replacementFilterSupplierComboBox.addItem(new ComboItem(null, "All"));
            reportsFilterSupplierComboBox.addItem(new ComboItem(null, "All"));
            adjustmentsSupplier.addItem(new ComboItem(null, "All"));
            //returnsSupplierComboBox.addItem(new ComboItem(null, "All"));
            while (rs.next()) {
                String supplierID = rs.getString(1);
                String supplierName = rs.getString(2);
                
                addProductSupplierComboBox.addItem(new ComboItem(supplierID, supplierName));
                editProductSupplierComboBox.addItem(new ComboItem(supplierID, supplierName));
                editProductFilterSupplierComboBox.addItem(new ComboItem(supplierID, supplierName));
                transferProductFilterSupplierComboBox.addItem(new ComboItem(supplierID, supplierName));
                deliveryProductFilterSupplierComboBox.addItem(new ComboItem(supplierID, supplierName));
                productFilterSupplierComboBox.addItem(new ComboItem(supplierID, supplierName));
                replacementFilterSupplierComboBox.addItem(new ComboItem(supplierID, supplierName));
                reportsFilterSupplierComboBox.addItem(new ComboItem(supplierID, supplierName));
                adjustmentsSupplier.addItem(new ComboItem(supplierID, supplierName));                
                //returnsSupplierComboBox.addItem(new ComboItem(supplierID, supplierName));
            }
        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
            JOptionPane op = new JOptionPane("Failed to update supplier components.", JOptionPane.ERROR_MESSAGE);
            JDialog dialog = op.createDialog("Database Error");
            dialog.setAlwaysOnTop(true);
            dialog.setModal(true);
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
            //JOptionPane.showMessageDialog(null,"Failed to update Supplier Components.", "Database Error", JOptionPane.ERROR_MESSAGE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
